<?php

// @formatter:off
/**
 * @file WCParameters.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCParameters.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/WCParameters.php $
 */
// @formatter:on

/**
 * @class WCParameters
 */
class WCParameters extends WCSingleton {
	/**
	 * @var WCConnection Pointer to the global connection object.
	 */
	protected $_db = null;
	/**
	 * @var string Shortcut to database tables prefixes.
	 */
	protected $_dbprefix = "";
	protected $_params = array();
	/**
	 * Class basic constructor.
	 */
	protected function __construct() {
		//
		// Loading global pointers.
		global $wcDatabaseAccess;
		//
		// Loading shortcuts.
		// @{
		$this->_db = WCConnection::Instance();
		$this->_dbprefix = $wcDatabaseAccess["prefix"];
		// @}
	}
	//
	// Public methods.
	public function __get($name) {
		$out = null;

		$this->load($name);
		if(isset($this->_params[$name])) {
			$out = $this->_params[$name];
		}

		return $out;
	}
	public function __set($name, $value) {
		if($this->autoCreate($name)) {
			//
			// Creating a statement to update a parameter.
			// @{
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "update  {$this->_dbprefix}parameters\n";
				$query .= "set     prm_value = :value\n";
				$query .= "where   prm_name = :name\n";
				$stmt = $this->_db->prepare($stmtId, $query);
			}
			// @}
			$params = array(
				":name" => $name,
				":value" => $value
			);
			if($stmt->execute($params) && $stmt->rowCount() > 0) {
				unset($this->_params[$name]);
			}
		}

		return $this->$name;
	}
	//
	// Protected methods.
	protected function autoCreate($name) {
		$created = true;

		$this->load($name);
		if(!isset($this->_params[$name])) {
			$created = false;
			//
			// Creating a statement to insert a new parameter.
			// @{
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "insert\n";
				$query .= "        into {$this->_dbprefix}parameters (\n";
				$query .= "                prm_name, prm_value, prm_active)\n";
				$query .= "        values (:name, '', 'Y')\n";
				$query .= "                on duplicate key\n";
				$query .= "                        update prm_active = 'Y'\n";
				$stmt = $this->_db->prepare($stmtId, $query);
			}
			// @}

			if($stmt->execute(array(":name" => $name)) && $stmt->rowCount() > 0) {
				$this->load($name);
				$created = true;
			}
		}

		return $created;
	}
	protected function load($name) {
		if(!isset($this->_params[$name])) {
			//
			// Creating a statement to get a parameter.
			// @{
			$stmt = null;
			$stmtId = __CLASS__."::".__FUNCTION__;
			if($this->_db->has($stmtId)) {
				$stmt = $this->_db->get($stmtId);
			} else {
				$query = "select  *\n";
				$query .= "from    {$this->_dbprefix}parameters\n";
				$query .= "where   prm_name   = :name\n";
				$query .= " and    prm_active = 'Y'\n";
				$stmt = $this->_db->prepare($stmtId, $query);
			}
			// @}

			if($stmt->execute(array(":name" => $name)) && $stmt->rowCount() > 0) {
				$row = $stmt->fetch();
				$this->_params[$name] = $row["prm_value"];
			}
			$stmt->closeCursor();
		}
	}
	//
	// Public class methods.
}
